package com.dpmc.service;
  
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;

import bsh.Interpreter;

import com.common.Arith;
import com.common.util.ObjectUtils;
import com.core.dao.BaseQuery;
import com.core.service.AppBaseService;  
import com.dpmc.entity.*; 
/**
 * 服务类——清算系统
 */
@Stateless
public class SVC_QSXT  extends AppBaseService implements SVC_QSXTRemote, SVC_QSXTLocal {
	
	/**取规则ID**/
	private String getYJFPGZ_ID(Map param){
		String yjfpgz_id=null;
		List statements = getResultList("Q_GZGL_YJFPGZ_FZDY.GET_TB_BASE_STATEMENT"); 
		HashSet tmpValuesSet = new HashSet(); 
		HashSet tmpValuesSet2 = new HashSet(); 
		for(Object item :statements){
			Map m = (Map)item;
			String statement = getStatement((String) m.get("CONTENT"), param,null);
		 
			BaseQuery query =getQuery((String)m.get("TYPE"));
			List values = query.getResultList(statement, param);
			if(values.size()<1){
				yjfpgz_id = null;
				break;
			}
			if(values.size()==1){
				Map m_v = (Map)values.get(0);
				if(m_v.get("YJFPGZ_ID")==null){
					yjfpgz_id = null;
					break;
				}
			}
			if(tmpValuesSet.size()==0){
				for(Object o_v :values){
					Map m_v = (Map)o_v;
					if(m_v.get("YJFPGZ_ID")!=null){
					   tmpValuesSet.add(m_v.get("YJFPGZ_ID"));
					}
				}
			}else{ 
				 for(Object o_v :values){
					 Map m_v = (Map)o_v;
					 if(tmpValuesSet.contains(m_v.get("YJFPGZ_ID"))){
						 tmpValuesSet2.add(m_v.get("YJFPGZ_ID"));
					 }
				 } 
				 tmpValuesSet.clear();
				 tmpValuesSet.addAll(tmpValuesSet2);
				 tmpValuesSet2.clear();
			}
		}
		if(tmpValuesSet.size()==1){
			yjfpgz_id = (String) tmpValuesSet.iterator().next();
		} 
		return yjfpgz_id;
	}

	@Override
	public void doClearing(Map param) { 
		/**保存刷卡交易记录   begin **/
		TB_QSXT_SKXXJL skxxjl =  ObjectUtils.mapToJavaBean(param, TB_QSXT_SKXXJL.class);
		skxxjl.setID(skxxjl.uuid());
		Date optime =getOpTime(param);
		getJpaDao().save(skxxjl); 
		
		/**一次清算   begin **/
		//商户协议信息表 
		Map m_HYZK_YJBL = (Map) querySingleResult("Q_QSXT.GET_SHID_YJBL_BY_POSJHM",param); 
		//佣金收入记录
		Double yjbl = ObjectUtils.getMapValue(m_HYZK_YJBL, "YJBL", Double.class);
		Double YJJE = Arith.mul(skxxjl.getSKJE(), yjbl/100) ;
		TB_QSXT_YJSRJL yjsrjl = new TB_QSXT_YJSRJL();
		yjsrjl.setID(yjsrjl.uuid());
		yjsrjl.setZDH(skxxjl.getZDH());
		yjsrjl.setRZJE(YJJE);
		yjsrjl.setRZSJ(optime);
		getJpaDao().save(yjsrjl);
	    //商户收入记录
		String shid = ObjectUtils.getMapValue(m_HYZK_YJBL, "SHID", String.class);
		TB_QSXT_SHSRJL shsrjl = new TB_QSXT_SHSRJL();
		shsrjl.setID(shsrjl.uuid());
		shsrjl.setSJID(shid);
		shsrjl.setZDH(skxxjl.getZDH());
		shsrjl.setRZJE(  Arith.sub(skxxjl.getSKJE(), YJJE) );
		shsrjl.setRZSJ(optime);
		getJpaDao().save(shsrjl);
		
		getServiceInterface().doService("SVC_CWGL", "RZJY", "TB_SHGL_SHJBXX",shid,"TB_QSXT_SHSRJL",shsrjl.getID(),shsrjl.getRZSJ(),shsrjl.getRZJE());

		/**一次清算   end **/
		
		//平台佣金
		Double d_PTYJJE = YJJE ;
		
		//获取该卡片类型，该商家类型的佣金分配规则
		//取规则ID
		String yjfpgz_id=getYJFPGZ_ID(param);  
		if(yjfpgz_id==null){
			return;
		} 
		param.put("YJFPGZ_ID", yjfpgz_id);
		List<Map> yjfpgzx_list = (List<Map>) getResultList("Q_QSXT.GET_FPFLX_ID_FPJSGS_JDWS_SRMS_BY_YJFPGZ_ID",param); 
		for(Map item_:yjfpgzx_list){
			TB_GZGL_YJFPGZX gzx =  ObjectUtils.mapToJavaBean(item_, TB_GZGL_YJFPGZX.class);
			TB_QSXT_YJFPJL yjfpjl = new TB_QSXT_YJFPJL();
			yjfpjl.setID(yjfpjl.uuid());
			yjfpjl.setYJSRID(yjsrjl.getID());
			yjfpjl.setRZSJ(optime); 
			String FPJSGS = "Double value = 0; "+gzx.getFPJSGS();
			FPJSGS = FPJSGS.replaceAll("YJJE", YJJE.toString());
			FPJSGS = FPJSGS.replaceAll("SKJE", ((Double)skxxjl.getSKJE()).toString());
			FPJSGS = FPJSGS.replaceAll("SHSRJE", ((Double)shsrjl.getRZJE()).toString());
			try { 
			      Interpreter interpreter = new Interpreter(); // 构造一个解释器 
			      interpreter.eval(FPJSGS); // 对一段脚本求值，并得到结果
			      Double yj = (Double) interpreter.get("value");
			      if(gzx.getJDWS()!=null){
			    	  yj=  Arith.round(yj.doubleValue(), gzx.getJDWS(), gzx.getSRMS());
			      } 
			      yjfpjl.setRZJE(yj); 
			}catch (Exception e) { 
			      throw new RuntimeException(e);
			} 
			
			
			yjfpjl.setFQFLX(gzx.getFPFLX_ID());
			param.put("FPFLX_ID",gzx.getFPFLX_ID());
			
			Map m_value = (Map)querySingleResult("Q_QSXT.GET_VALUE_VALUE_TYPE_CODE_BY_FPFLX_ID",param);
			String s_VALUE = ObjectUtils.getMapValue(m_value, "VALUE", String.class);
			String s_VALUE_TYPE = ObjectUtils.getMapValue(m_value, "VALUE_TYPE", String.class);
			String s_CODE =  ObjectUtils.getMapValue(m_value, "CODE", String.class);
			
			param.put("SHID", shid);
			String fpfid = null;
			if("value_string".equals(s_VALUE_TYPE)){
				 fpfid = ObjectUtils.getMapValue(param, s_VALUE, String.class);
			}else if("value_statement".equals(s_VALUE_TYPE)){
				fpfid = (String)querySingleResult(s_VALUE,param);
			}  
			if(fpfid!=null){
				d_PTYJJE = Arith.sub(d_PTYJJE, yjfpjl.getRZJE());  
			   yjfpjl.setFQFID(fpfid); 
			   getJpaDao().save(yjfpjl);
			   getServiceInterface().doService("SVC_CWGL", "RZJY", s_CODE,fpfid,"TB_QSXT_YJFPJL",yjfpjl.getID(),yjfpjl.getRZSJ(),yjfpjl.getRZJE());
			}
		} 
		
		TB_QSXT_YJFPJL yjfpjl_pt = new TB_QSXT_YJFPJL();
		yjfpjl_pt.setID(yjfpjl_pt.uuid());
		yjfpjl_pt.setYJSRID(yjsrjl.getID());
		yjfpjl_pt.setRZSJ(optime); 
		yjfpjl_pt.setFQFID("0");
		yjfpjl_pt.setFQFLX("0");
		yjfpjl_pt.setRZJE(d_PTYJJE);
		getJpaDao().save(yjfpjl_pt);
		
		getServiceInterface().doService("SVC_CWGL", "RZJY", "SYS_ACCOUNT","0","TB_QSXT_YJFPJL",yjfpjl_pt.getID(),yjfpjl_pt.getRZSJ(),yjfpjl_pt.getRZJE());
	}  
	
}
